<p><strong>CSpec : Quick Start</strong></p>
<p>Cspec is a testing framework inspired by ruby RSpec, and more behavioral driven approach.</p>
<p>The main goal of this project is to have a very fluent testing framework, that can be expressive and replicate the real language as close as it can.</p>
<p>It uses a combination of extension methods, lambda parsing and custom delegates to replicate fluent language and behavioral pattern.</p>
<p><strong>CSpec : Overview</strong></p>
<p>Cspec as a framework consists of two seperate components, the first one is the CSpec istelf and the second one</p>
<p>is a TestRunner console. One can use the extension features of CSpec without the TestRunner or</p>
<p>even write it's own test runner using CSpec but the full feature set</p>
<p>is used when testing with the default test runner.</p>
<p>To be able to run tests on a assembly or type, the first thing we need to do&nbsp;</p>
<p>is to create a Spec class of the&nbsp;original&nbsp;type, then define it's behavior and then</p>
<p>run it in test runner.</p>
<p><strong>CSpec : Basics</strong></p>
<p>The most important feature of CSpec lays in the Tags.</p>
<p>A Tag is special lambda variable that is parsed and then executed</p>
<p>in the context of the variable.</p>
<p>&nbsp;</p>
<p>Most of tags are executed&nbsp;through&nbsp;single Extension method :</p>
<div style="color: black; background-color: white;">
<pre style="color: black;"><span style="color: blue;">public</span> <span style="color: blue;">static</span> T Should&lt;T, N&gt;(<span style="color: blue;">this</span> T obj, Expression&lt;Func&lt;T, N&gt;&gt; operation)</pre>
<pre style="color: black;"><span style="color: blue;">public</span> <span style="color: blue;">static</span> T Should&lt;T, N&gt;(<span style="color: blue;">this</span> T obj, Expression&lt;Func&lt;T, N&gt;&gt; operation, N operand)</pre>
<pre style="color: black;"><br /></pre>
</div>
<p>&nbsp;</p>
<p>This extension method needs to be called with specified tags to behave in a certain expected way, if wrong</p>
<p>tags will be passed it will fail.</p>
<p>&nbsp;</p>
<p><span style="text-decoration: underline;">Example usage:</span></p>
<p>&nbsp;</p>
<div style="color: black; background-color: white;">
<pre>obj.Method(<span style="color: #a31515;">"Hello"</span>).Should(@be =&gt; 1);</pre>
</div>
<p>&nbsp;</p>
<p>This indicates that Method return value should be equal to 1.</p>
<p>There are more tags like:&nbsp;</p>
<p>@be:&nbsp;&nbsp;It's used to indicate that a type or return type should be equal to something.</p>
<p>@have:&nbsp;&nbsp;It's used on booleans and object.&nbsp;When it's called on objects properties it does null checks on those properties.&nbsp;When called on booleans it check if some return type or value is true.</p>
<p>@not_be:&nbsp;&nbsp;negation of @be tag.</p>
<p>@contain:&nbsp;It's used on collections and arrays to check for a specified item.&nbsp;@rcontain:&nbsp;It's used on collections and arrays to check for a specified item but using reflection matching&nbsp;instead of reference.</p>
<p>@raise: [Class must implement Interface]&nbsp;It's used on methods to instrut that a certain set of parameter data&nbsp;should throw a specified exception.</p>
<p>@not_raise: [Class must implement Interface]&nbsp;It's used on methods to instrut that a certain set of parameter data&nbsp;should not throw a specified exception.</p>
<p>@be_close:&nbsp;&nbsp;It's used on numbers and represents the logical rule actual == expected +/- delta</p>
<p>Tags are not strong typed as they are name variables that get parsed on&nbsp;run time.&nbsp;Although&nbsp;they are the&nbsp;preferred&nbsp;way of writing expectations there</p>
<p>are also strong typed tags (under namespace <span style="text-decoration: underline;">CSpec.Extensions.Tags)</span>&nbsp;that are C# methods with arguments but hey break the fluent design of behavioral testing, but users still might use them.</p>
<p>&nbsp;</p>
<p>Example:</p>
<p>Lets create a sample component, that will be a calculator with public interface&nbsp;</p>
<p>First the interface:</p>
<p>
<div style="color: black; background-color: white;">
<pre>    <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
    <span style="color: gray;">///</span><span style="color: green;"> A very simple calc interface</span>
    <span style="color: gray;">///</span><span style="color: green;"> with value carry over.</span>
    <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
    <span style="color: blue;">public</span> <span style="color: blue;">interface</span> ICalc
    {
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Adds a value to the total.</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;param name="a"&gt;</span><span style="color: gray;">&lt;/param&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;returns&gt;</span><span style="color: gray;">&lt;/returns&gt;</span>
        <span style="color: blue;">int</span> Sum(<span style="color: blue;">int</span> a);
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Substracts a value from total.</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;param name="a"&gt;</span><span style="color: gray;">&lt;/param&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;returns&gt;</span><span style="color: gray;">&lt;/returns&gt;</span>
        <span style="color: blue;">int</span> Sub(<span style="color: blue;">int</span> a);
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Takes a total to a power of a given value.</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;param name="a"&gt;</span><span style="color: gray;">&lt;/param&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;returns&gt;</span><span style="color: gray;">&lt;/returns&gt;</span>
        <span style="color: blue;">int</span> Pow(<span style="color: blue;">int</span> a);
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Divides the total by a given value.</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;param name="a"&gt;</span><span style="color: gray;">&lt;/param&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;returns&gt;</span><span style="color: gray;">&lt;/returns&gt;</span>
        <span style="color: blue;">int</span> Div(<span style="color: blue;">int</span> a);

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Gets/Sets the total value.</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: blue;">int</span> Total { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Rests the total.</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: blue;">void</span> Reset();
        
    }</pre>
</div>
</p>
<p>Then let's implement the interface:</p>
<p>&nbsp;</p>
<div style="color: black; background-color: white;">
<pre>    <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
    <span style="color: gray;">///</span><span style="color: green;"> Calculator project to test CSpec on </span>
    <span style="color: gray;">///</span><span style="color: green;"> a (semi) real project, rater then </span>
    <span style="color: gray;">///</span><span style="color: green;"> just testing methods.</span>
    <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
    <span style="color: blue;">public</span> <span style="color: blue;">class</span> Calc : ICalc
    {
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Initalisation constructor</span>
        <span style="color: gray;">///</span><span style="color: green;"> resets the Total value.</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: blue;">public</span> Calc()
        {
            Total = 0;
        }

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Adds a value to the total</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;param name="a"&gt;</span><span style="color: gray;">&lt;/param&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;returns&gt;</span><span style="color: gray;">&lt;/returns&gt;</span>
        <span style="color: blue;">public</span> <span style="color: blue;">int</span> Sum(<span style="color: blue;">int</span> a)
        {
            Total += a;
            <span style="color: blue;">return</span> Total;
        }

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Substracts a value from total</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;param name="a"&gt;</span><span style="color: gray;">&lt;/param&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;returns&gt;</span><span style="color: gray;">&lt;/returns&gt;</span>
        <span style="color: blue;">public</span> <span style="color: blue;">int</span> Sub(<span style="color: blue;">int</span> a)
        {
            Total -= a;
            <span style="color: blue;">return</span> Total;
        }

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Takes a total to a power of a given value</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;param name="a"&gt;</span><span style="color: gray;">&lt;/param&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;returns&gt;</span><span style="color: gray;">&lt;/returns&gt;</span>
        <span style="color: blue;">public</span> <span style="color: blue;">int</span> Pow(<span style="color: blue;">int</span> a)
        {
            Total = (<span style="color: blue;">int</span>)Math.Pow(Total, a);
            <span style="color: blue;">return</span> Total;
        }

        <span style="color: green;">/*
         * There are many things that can go wrong with the div 
         * method in this context.
         * 
         * So we will check if our crappy metthod will throw exceptions.
         */</span>

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Divides the total by a given value.</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;param name="a"&gt;</span><span style="color: gray;">&lt;/param&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;returns&gt;</span><span style="color: gray;">&lt;/returns&gt;</span>
        <span style="color: blue;">public</span> <span style="color: blue;">int</span> Div(<span style="color: blue;">int</span> a)
        {
            Total = Total / a;
            <span style="color: blue;">return</span> Total;
        }

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Gets the total value</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: blue;">public</span> <span style="color: blue;">int</span> Total
        {
            <span style="color: blue;">get</span>;
            <span style="color: blue;">set</span>;
        }

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Rests the total</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: blue;">public</span> <span style="color: blue;">void</span> Reset()
        {
            Total = 0;
        }
    }</pre>
</div>
<p>&nbsp;</p>
<p>Now that we have our type defined we need to create a new project (not obligatory but good practice)</p>
<p>called CalcSpec, and we need to reference our&nbsp;original&nbsp;component.</p>
<p>The Speced classes use a pattern called Facade to define behaviors and operations that the oryginal component should</p>
<p>follow.</p>
<p>Lets create a Spec class like so:</p>
<p>
<div style="color: black; background-color: white;">
<pre>    <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
    <span style="color: gray;">///</span><span style="color: green;"> Spec type to a Calculator project</span>
    <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
    <span style="color: gray;">///</span> <span style="color: gray;">&lt;remarks&gt;</span>
    <span style="color: gray;">///</span><span style="color: green;"> You don't need to use an interface type to use</span>
    <span style="color: gray;">///</span><span style="color: green;"> CSpec regullar Calc class would be suficient,</span>
    <span style="color: gray;">///</span><span style="color: green;"> but when using the interface type, you gent additional</span>
    <span style="color: gray;">///</span><span style="color: green;"> tags like @raise, @not_raise.</span>
    <span style="color: gray;">///</span><span style="color: green;"> </span>
    <span style="color: gray;">///</span><span style="color: green;"> Why is it so?</span>
    <span style="color: gray;">///</span><span style="color: green;"> </span>
    <span style="color: gray;">///</span><span style="color: green;"> Only interface Types give the ability to do at as they can</span>
    <span style="color: gray;">///</span><span style="color: green;"> be Proxied, thus advanced dynamic operations are possible</span>
    <span style="color: gray;">///</span><span style="color: green;"> in this context we can do (pseudo) backwards lookahead.</span>
    <span style="color: gray;">///</span> <span style="color: gray;">&lt;/remarks&gt;</span>
    <span style="color: blue;">public</span> <span style="color: blue;">class</span> CalcSpec : CSpecFacade&lt;ICalc&gt;
    {
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Initializes the parametric constructor of Cspec</span>
        <span style="color: gray;">///</span><span style="color: green;"> and passes the constructor of the Specd class</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;remarks&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> You can use the default constructor if the creation of the parametric one is a chore</span>
        <span style="color: gray;">///</span><span style="color: green;"> (when your class doesn't have simple constructors but require tons of operations to be created),</span>
        <span style="color: gray;">///</span><span style="color: green;"> and just call InitializeFacade</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/remarks&gt;</span>
        <span style="color: blue;">public</span> CalcSpec()
            : <span style="color: blue;">base</span>(<span style="color: blue;">new</span> Calc())
        {
        }

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Clean the Total after each operaton.</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: blue;">protected</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span>  BeforeOperation()
        {
 	        ObjSpec.Total = 0;
        }

        <span style="color: green;">//MS aparently has a bug in the code.</span>
        <span style="color: green;">//without this nothing will work and the DESCRIBE ALL delegate will throw exception.</span>
        <span style="color: green;">//In mono it works perfect.</span>
        <span style="color: blue;">private</span> Action&lt;<span style="color: blue;">string</span>&gt; dummy = <span style="color: blue;">null</span>;

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Simple addition testing</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        DescribeAll describe_sum =
            (@it, @<span style="color: blue;">do</span>) =&gt;
            {
                @it(<span style="color: #a31515;">"Adds a value of 5 to total"</span>);
                @<span style="color: blue;">do</span>.Sum(5).Should(@be =&gt; 5);
            };


        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Simple substraction testing</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        DescribeAll describe_sub =
            (@it, @<span style="color: blue;">do</span>) =&gt;
            {
                @it(<span style="color: #a31515;">"Substracts a value of 5 from total"</span>);
                @<span style="color: blue;">do</span>.Sub(5).Should(@be =&gt; -5);
            };

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Simple power testing</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;remarks&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> There is a catch here AHA!, the result of </span>
        <span style="color: gray;">///</span><span style="color: green;"> calling POW needs to be converted to int,</span>
        <span style="color: gray;">///</span><span style="color: green;"> in this context this is simple but you have to watch for this.</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/remarks&gt;</span>
        DescribeAll describe_pow =
            (@it, @<span style="color: blue;">do</span>) =&gt;
            {
                @it(<span style="color: #a31515;">"Takes the total to a power of 5"</span>);
                @<span style="color: blue;">do</span>.Total = 5;
                @<span style="color: blue;">do</span>.Pow(5).Should(@be =&gt; (<span style="color: blue;">int</span>)Math.Pow(5,5));
            };

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Testing of Div, and expecting exceptions</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        DescribeAll describe_div =
            (@it, @<span style="color: blue;">do</span>) =&gt;
            {
                @it(<span style="color: #a31515;">"Divides the total by a value of 5"</span>);
                @it(<span style="color: #a31515;">"In this context Divide should be ok"</span>);
                @<span style="color: blue;">do</span>.Div(5).Should(@not_raise =&gt; <span style="color: blue;">typeof</span>(DivideByZeroException));

                @it(<span style="color: #a31515;">"Divides the total by a value of 0"</span>);
                @it(<span style="color: #a31515;">"In this context Divide should be wrong and throw an exception"</span>);
                @<span style="color: blue;">do</span>.Div(0).Should(@raise =&gt; <span style="color: blue;">typeof</span>(DivideByZeroException));
            };

        <span style="color: gray;">///</span> <span style="color: gray;">&lt;summary&gt;</span>
        <span style="color: gray;">///</span><span style="color: green;"> Testing the value that needs to fit some range.</span>
        <span style="color: gray;">///</span> <span style="color: gray;">&lt;/summary&gt;</span>
        DescribeAll describe_range =
            (@it, @<span style="color: blue;">do</span>) =&gt;
            {
                Random rnd = <span style="color: blue;">new</span> Random();
                <span style="color: blue;">var</span> value = rnd.Next(1, 5);

                @it(<span style="color: #a31515;">"Adds a random value from 1 to 5, current value: "</span> + value);
                @it(<span style="color: #a31515;">"In this case the total should be in a specified range"</span>);

                @<span style="color: blue;">do</span>.Sum(value);
                @<span style="color: blue;">do</span>.Total.Should(@be_close =&gt; 1, 5); 
            };
    }</pre>
</div>
</p>
<p>The inherited CSpecFacade contains special deletates that describe the operations, those operations will be used by</p>
<p>test runner and evaluated.</p>
<p>&nbsp;</p>
<p>Each operation has two parameters @it, @do.</p>
<p>@it contains a description of the operation</p>
<p>@do is the Spec(ed) class&nbsp;</p>
<p>&nbsp;</p>
<p><strong>CSpec : Testing</strong></p>
<p>After everything is se run the CSpec.Console and point it to Speced class</p>
<p>and the test will commence.</p>
<p>
<div style="color: black; background-color: white;">
<pre>CSpec.Console.exe runner MyAssemblyContainingSpec.dll -a
</pre>
</div>
</p>
<p>It is important to know that the path to the assembly must be absolute.</p>
<p>For help with the Console type:</p>
<p>
<div style="color: black; background-color: white;">
<pre>CSpec.Console.exe help
</pre>
<pre><br /></pre>
<pre><strong>CSpec : Ending</strong></pre>
</div>
</p>
<p>This basic guide should get you started on CSpec it does not cover the framework</p>
<p>and it's features in a lot of detail so the best way to know more would</p>
<p>be to look at the class documentation and code comments</p>
<p>as they contain very detailed information.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong><br /></strong></p>